package com.github.m6d21.sso.business.controller.permission;

import com.github.m6d21.sso.common.util.EmptyUtil;
import com.github.m6d21.sso.model.permission.entity.Permission;
import com.github.m6d21.sso.model.permission.qo.PermissionQO;
import com.github.m6d21.sso.service.permission.IPermissionService;
import com.github.m6d21.sso.support.annotation.RequestArrayParam;
import com.github.m6d21.sso.support.annotation.RequestDateParam;
import com.github.m6d21.sso.support.enums.StatusCode;
import com.github.m6d21.sso.support.model.Response;
import com.github.m6d21.sso.support.util.ResponseUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *<p>
 *账号api
 *</p>
 *
 * @author liuyang
 * @date 2018-12-07 Fri 13:27:00
 * @since 1.0.0
 */
@Api(value = "permission", description = "权限api")
@Validated
@RestController
@RequestMapping("/web/permission")
public class PermissionController {

    @Resource
    private IPermissionService permissionService;

    @ApiOperation(value = "保存权限信息", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "权限名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "permissionCode", value = "权限编码", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "disable", value = "是否禁用", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "remark", value = "备注", paramType = "query", dataType = "string")})
    @PostMapping("/save")
    @ResponseBody
    public Response<Integer> savePermission(
        @NotBlank(message = "权限名称不能为空") @RequestParam("name") String name,
        @NotBlank(message = "权限编码不能为空") @RequestParam("permissionCode") String permissionCode,
        @RequestParam("disable") Integer disable,
        @RequestParam("remark") String remark) {
        Permission permission = new Permission();
        permission.setName(EmptyUtil.ifBlankToNull(name));
        permission.setPermissionCode(EmptyUtil.ifBlankToNull(permissionCode));
        permission.setDisable(disable);
        permission.setRemark(EmptyUtil.ifBlankToNull(remark));
        permission.setDisable(disable);
        permissionService.savePermission(permission);
        return ResponseUtil.success(permission.getId());
    }

    @ApiOperation(value = "查询权限信息", httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "权限名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "permissionCode", value = "权限编码", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "startDate", value = "创建时间", paramType = "query", dataType = "date"),
        @ApiImplicitParam(name = "endDate", value = "创建时间", paramType = "query", dataType = "date")})
    @GetMapping("/query/all")
    @ResponseBody
    public Response<List<Permission>> queryAllPermission(
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "permissionCode", required = false) String permissionCode,
        @RequestDateParam(value = "startDate") Date startDate,
        @RequestDateParam(value = "endDate") Date endDate) {
        PermissionQO permissionQO = new PermissionQO();
        permissionQO.setName(EmptyUtil.ifBlankToNull(name));
        permissionQO.setPermissionCode(EmptyUtil.ifBlankToNull(permissionCode));
        permissionQO.setStartDate(startDate);
        permissionQO.setEndDate(endDate);
        return ResponseUtil.success(permissionService.query(permissionQO));
    }


    @ApiOperation(value = "查询权限信息（分页）", httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "页码(当前页)", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "name", value = "权限名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "disable", value = "是否禁用", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "permissionCode", value = "权限编码", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "startDate", value = "创建时间", paramType = "query", dataType = "date"),
        @ApiImplicitParam(name = "endDate", value = "创建时间", paramType = "query", dataType = "date")})
    @GetMapping("/query")
    @ResponseBody
    public Response<PageInfo<Permission>> queryPermission(
        @RequestParam(value = "pageNo", required = false) Integer pageNo,
        @RequestParam(value = "pageSize", required = false) Integer pageSize,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "permissionCode", required = false) String permissionCode,
        @RequestDateParam(value = "startDate") Date startDate,
        @RequestDateParam(value = "endDate") Date endDate) {
        PermissionQO permissionQO = new PermissionQO();
        permissionQO.setName(EmptyUtil.ifBlankToNull(name));
        permissionQO.setPermissionCode(EmptyUtil.ifBlankToNull(permissionCode));
        permissionQO.setStartDate(startDate);
        permissionQO.setEndDate(endDate);
        return ResponseUtil.success(permissionService.queryByPage(pageNo, pageSize, permissionQO));
    }

    @ApiOperation(value = "查询权限信息（分页）", httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "页码(当前页)", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "name", value = "权限名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "disable", value = "是否禁用", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "permissionCode", value = "权限编码", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "startDate", value = "创建时间", paramType = "query", dataType = "date"),
        @ApiImplicitParam(name = "endDate", value = "创建时间", paramType = "query", dataType = "date")})
    @GetMapping("/query/for/admin")
    @ResponseBody
    public Response<PageInfo<Permission>> queryPermissionForAdmin(
        @RequestParam(value = "pageNo", required = false) Integer pageNo,
        @RequestParam(value = "pageSize", required = false) Integer pageSize,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "disable", required = false) Integer disable,
        @RequestParam(value = "permissionCode", required = false) String permissionCode,
        @RequestDateParam(value = "startDate") Date startDate,
        @RequestDateParam(value = "endDate") Date endDate) {
        PermissionQO permissionQO = new PermissionQO();
        permissionQO.setName(EmptyUtil.ifBlankToNull(name));
        permissionQO.setPermissionCode(EmptyUtil.ifBlankToNull(permissionCode));
        permissionQO.setDisable(disable);
        permissionQO.setStartDate(startDate);
        permissionQO.setEndDate(endDate);
        return ResponseUtil.success(permissionService.queryByPageForAdmin(pageNo, pageSize, permissionQO));
    }

    @ApiOperation(value = "修改权限信息（包含权限信息）", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "权限名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "permissionCode", value = "权限编码", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "disable", value = "是否禁用", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "remark", value = "备注", paramType = "query", dataType = "string")})
    @PostMapping("/modify")
    @ResponseBody
    public Response<Permission> updatePermission(
        @NotNull(message = "权限id不能为空") @RequestParam("id") Integer id,
        @RequestParam("name") String name,
        @RequestParam("permissionCode") String permissionCode,
        @RequestParam("disable") Integer disable,
        @RequestParam("remark") String remark) {
        Permission permission = new Permission();
        permission.setId(id);
        permission.setName(EmptyUtil.ifBlankToNull(name));
        permission.setPermissionCode(EmptyUtil.ifBlankToNull(permissionCode));
        permission.setDisable(disable);
        permission.setRemark(EmptyUtil.ifBlankToNull(remark));
        permission.setDisable(disable);

        return ResponseUtil.success(permissionService.modifyPermissionById(permission));
    }

    @ApiOperation(value = "根据权限ID删除权限", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "权限ID", paramType = "query", dataType = "int")
    @PostMapping("/delete/id")
    @ResponseBody
    public Response deletePermissionById
        (@NotNull(message = "权限ID不能为空") @RequestParam("id") Integer id) {
        Boolean success = permissionService.deletePermissionById(id);
        if (!success) {
            /**
             * @see StatusCode
             */
            return ResponseUtil.fail(StatusCode.FAIL.getCode(), "删除失败");
        }
        return ResponseUtil.success();
    }

    @ApiOperation(value = "根据权限ID批量删除权限", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "权限ID数组", paramType = "query", dataType = "array")
    @PostMapping("/delete/ids")
    @ResponseBody
    public Response deletePermissionByIds
        (@NotNull(message = "权限ID不能为空") @RequestArrayParam(value = "ids", type = Integer.class) Integer[] ids) {
        Boolean success = permissionService.deletePermissionByIds(Lists.newArrayList(ids));
        if (!success) {
            return ResponseUtil.fail(StatusCode.FAIL.getCode(), "删除失败");
        }
        return ResponseUtil.success();
    }

    @ApiOperation(value = "查询权限编码是否存在", httpMethod = "GET")
    @ApiImplicitParam(name = "permissionCode", value = "权限编", paramType = "query", dataType = "string")
    @GetMapping("/exist/code")
    @ResponseBody
    public Response existByUsername(@RequestParam(value = "permissionCode", required = false) String permissionCode) {
        Boolean exist = permissionService.existByPermissionCode(permissionCode);
        Map<String, Boolean> data = new HashMap<>();
        data.put("exist", exist);
        return ResponseUtil.success(data);
    }
}
